<?php

include_once dirname(__DIR__).'/public_models.php';		//加载数据库操作类
class Manage_Model extends Public_Models
{
	public function __construct()
	{
		header('Content-Type: text/html; charset=utf-8'); 
		parent::__construct();
	}

    public function getManageList($account_id){
        $dealerDB = Game_CONST::DBConst_Name;
        $result = array();

        $account_sql = 'select m.manager_id,m.member_id,a.* from '.Manage_Member.' m left join '.WX_Account.' a on a.account_id = m.manager_id  where m.status = 1 and  a.is_manage_on = 1 and m.account_id='.$account_id;
        $account_query = $this->getDataBySql($dealerDB,0,$account_sql);

        if(DB_CONST::DATA_NONEXISTENT == $account_query)
        {
            return array('result'=>OPT_CONST::FAILED,'data'=>$result,'result_message'=>"未加入任何工会");
        }
        else
        {
            return array('result'=>OPT_CONST::SUCCESS,'data'=>$account_query,'result_message'=>"加入的工会");
        }
    }

    public function getManageInfo($account_id,$manager_id){
        $dealerDB = Game_CONST::DBConst_Name;
        $account_sql = 'select * from '.Manage_Member.' where status = 1 and manager_id= '.$manager_id.' and account_id='.$account_id;
        $account_query = $this->getDataBySql($dealerDB,1,$account_sql);
        if(DB_CONST::DATA_NONEXISTENT == $account_query) {
            return array('result'=>OPT_CONST::FAILED,'data'=>[],'result_message'=>"");
        } else {
            return array('result'=>OPT_CONST::SUCCESS,'data'=>$account_query,'result_message'=>"");
        }
    }

	/*
		开启/关闭管理功能
	*/
	public function setManageSwitch($arrData)
	{
		$result = array();
		$timestamp = time();

		if(!isset($arrData['is_on']) || $arrData['is_on'] === "" )
		{
			log_message('error', "function(setManageSwitch):lack of is_on"." in file".__FILE__." on Line ".__LINE__);
			return $this->missingPrameterArr("is_on");
		}
		if(!isset($arrData['account_id']) || $arrData['account_id'] === "" )
		{
			log_message('error', "function(setManageSwitch):lack of account_id"." in file".__FILE__." on Line ".__LINE__);
			return $this->missingPrameterArr("account_id");
		}

		$is_on = $arrData['is_on'];
		$account_id = $arrData['account_id'];

		$dealerDB = Game_CONST::DBConst_Name;	

		$update_array['update_time'] = $timestamp;
		$update_array['update_appid'] = "aid_".$account_id;
		$update_array['is_manage_on'] = $is_on;
		$update_query = $this->updateFunc($dealerDB,"account_id",$account_id, WX_Account, $update_array);

		//扣除房卡
		if($is_on)
		{
			//将房卡添加到自己账户
			$updateTicket_str = 'update_time='.$timestamp.',update_appid="aid_'.$account_id.'",ticket_count=ticket_count-100';
			$updateTicket_where = 'account_id='.$account_id;
			//$updateTicket_query = $this->changeNodeValue($dealerDB,Room_Ticket,$updateTicket_str,$updateTicket_where);
			//房卡流水账
			 $journal_ary['journal_type'] = Game_CONST::JournalType_Disburse;
			 $journal_ary['account_id'] = $account_id;
			 $journal_ary['object_type'] = Game_CONST::ObjectType_Manage;
			 $journal_ary['object_id'] = -1;
			 $journal_ary['ticket_count'] = 100;
			 $journal_ary['extra'] = "";
			 $this->updateRoomTicketJournal($journal_ary,$dealerDB);

		}

		$result['is_on'] = $is_on;
		return array('result'=>OPT_CONST::SUCCESS,'data'=>$result, 'result_message'=>"设置成功");
	}


    /*
		获取邀请函内容
	*/
	public function getInviteData($arrData)
	{
		$timestamp = time();
		$result = array();
		
		if(!isset($arrData['code']) || $arrData['code'] == G_CONST::EMPTY_STRING)
        {
            log_message('error', "function(getInviteData):lack of code"." in file".__FILE__." on Line ".__LINE__);
            return $this->missingPrameterArr("code");
        }
		if(!isset($arrData['account_id']) || $arrData['account_id'] == G_CONST::EMPTY_STRING)
        {
            log_message('error', "function(getInviteData):lack of account_id"." in file".__FILE__." on Line ".__LINE__);
            return $this->missingPrameterArr("account_id");
        }
        $dealerDB = Game_CONST::DBConst_Name;	

		$code = $arrData['code'];		
		$account_id = $arrData['account_id'];


		//用户邀请用户信息
		$account_where = 'user_code="'.$code.'"';
		$account_sql = 'select account_id,nickname,headimgurl,phone from '.WX_Account.' where '.$account_where.'';
		$account_query = $this->getDataBySql($dealerDB,1,$account_sql);
		if(DB_CONST::DATA_NONEXISTENT == $account_query)
		{
			log_message('error', "function(getUserInfo):account not exist:".$account_id." in file".__FILE__." on Line ".__LINE__);
			return array('result'=>OPT_CONST::FAILED,'data'=>$result,'result_message'=>"邀请用户不存在");
		}
		else
		{
			$invite_aid = $account_query['account_id'];
			$nickname = $account_query['nickname'];
			$headimgurl = $account_query['headimgurl'];
		}

		$result['nickname'] = $nickname;
		$result['headimgurl'] = $headimgurl;
		
		//是否本人
		$is_owner = ($invite_aid == $account_id) ?  1 : 0;

		//获取用户是否加入公会
		$invite_status = 0;
		$join_where = 'account_id='.$account_id.' and manager_id='.$invite_aid.' and is_delete=0';
		$join_sql = 'select member_id,status from '.Manage_Member.' where '.$join_where.' limit 1';
		$join_query = $this->getDataBySql($dealerDB,1,$join_sql);
		if(DB_CONST::DATA_NONEXISTENT != $join_query)
		{
			if($join_query['status'] == 0){
				$invite_status = 1;
			} else if($join_query['status'] == 1) {
				$invite_status = 2;
			} else {
				$invite_status = 0;
			}
			
		} 

		$result['is_owner'] = $is_owner;
		$result['invite_status'] = $invite_status;
		
		return array('result'=>OPT_CONST::SUCCESS,'data'=>$result,'result_message'=>"获取邀请函内容");
	}


	/*
		申请加入群组
	*/
	public function joinGroup($arrData)
	{
		$timestamp = time();
		$result = array();
	
		if(!isset($arrData['user_code']) || $arrData['user_code'] == G_CONST::EMPTY_STRING)
        {
            log_message('error', "function(joinGroup):lack of user_code"." in file".__FILE__." on Line ".__LINE__);
            return $this->missingPrameterArr("user_code");
        }
		if(!isset($arrData['account_id']) || $arrData['account_id'] == G_CONST::EMPTY_STRING)
        {
            log_message('error', "function(joinGroup):lack of account_id"." in file".__FILE__." on Line ".__LINE__);
            return $this->missingPrameterArr("account_id");
        }
        $dealerDB = Game_CONST::DBConst_Name;	

		$user_code = $arrData['user_code'];		
		$account_id = $arrData['account_id'];
		
		//获取邀请函管理员信息
		$where = 'user_code="'.$user_code.'"  and is_delete=0';
		$sql = 'select account_id from '.WX_Account.' where '.$where.' limit 1';
		$query = $this->getDataBySql($dealerDB,1,$sql);
		if(DB_CONST::DATA_NONEXISTENT == $query)
		{
			return array('result'=>OPT_CONST::FAILED,'data'=>$result,'result_message'=>"邀请用户不存在");
		}
		$manager_id = $query['account_id'];


		//获取用户是否加入公会
		$join_where = 'account_id='.$account_id.' and manager_id='.$manager_id;
		$join_sql = 'select member_id,status,is_delete from '.Manage_Member.' where '.$join_where.' limit 1';
		$join_query = $this->getDataBySql($dealerDB,1,$join_sql);
		if(DB_CONST::DATA_NONEXISTENT != $join_query)
		{
			if(1 == $join_query['status'] && $join_query['is_delete'] == 0)
			{
				$result['status'] = 1;
				return array('result'=>OPT_CONST::SUCCESS,'data'=>$result,'result_message'=>"已加入该群组");
			}
			else
			{
				if($join_query['status']==2||$join_query['status']==3)
				{
					$member_id = $join_query['member_id'];
					$updateMember_str = 'update_time='.$timestamp.',update_appid="aid_'.$account_id.'",status=0,is_delete=0';
					$updateMember_where = 'member_id='.$member_id;
					$updateMember_query = $this->changeNodeValue($dealerDB,Manage_Member,$updateMember_str,$updateMember_where);
				}
				$result['status'] = 0;
				return array('result'=>OPT_CONST::SUCCESS,'data'=>$result,'result_message'=>"等待管理员确认");
			}
		}
		else
		{
			$insert_array['create_time'] = $timestamp;
			$insert_array['create_appid'] = "aid_".$account_id;
			$insert_array['update_time'] = $timestamp;
			$insert_array['update_appid'] = "aid_".$account_id;
			$insert_array['is_delete'] = 0;
			$insert_array['account_id'] = $account_id;
			$insert_array['manager_id'] = $manager_id;
			$insert_array['user_code'] = $user_code;
			$insert_array['status'] = 0;
			$member_id = $this->getInsertID($dealerDB,Manage_Member, $insert_array);

			$result['status'] = 0;
			return array('result'=>OPT_CONST::SUCCESS,'data'=>$result,'result_message'=>"等待管理员确认");
		}
	}


	/*
		查找成员
	*/
	public function searchMember($arrData)
	{
		$result = array();
		$timestamp = time();

		if(!isset($arrData['manager_id']) || $arrData['manager_id'] === "" )
		{
			log_message('error', "function(searchMember):lack of manager_id"." in file".__FILE__." on Line ".__LINE__);
			return $this->missingPrameterArr("manager_id");
		}
		if(!isset($arrData['page']))
		{
			log_message('error', "function(searchMember):lack of page"." in file".__FILE__." on Line ".__LINE__);
			return $this->missingPrameterArr("page");
		}

		$manager_id = $arrData['manager_id'];
		$nickname = isset($arrData['nickname']) ? $arrData['nickname'] : '' ;
		$dealerDB = Game_CONST::DBConst_Name;	

		$page = $arrData['page'];
		if($page == 0 || $page == "")
		{
			$page = 1;
		}
		$limit = 20;
		$offset = ($page - 1) * $limit;
		$sum_page = 1;
		$sum_count = 0;

		$where = WX_Account.'.account_id ='.Manage_Member.'.account_id and manager_id ='.$manager_id.' and '.Manage_Member.'.is_delete=0 ';
		if($nickname){
			$where .= ' and nickname like "%'.$nickname.'%" ';
		}

		$sql = 'select '.WX_Account.'.account_id,nickname,headimgurl,'.Manage_Member.'.create_time,member_id,status,is_admin  from '.WX_Account.','.Manage_Member.' where '.$where.' order by '.Manage_Member.'.update_time desc limit '.$offset.','.$limit;
		$list_query = $this->getDataBySql($dealerDB,0,$sql);
		if($list_query != DB_CONST::DATA_NONEXISTENT)
		{
			$count_sql = 'select count(1) as count from '.WX_Account.','.Manage_Member.' where '.$where;
			$count_query = $this->getDataBySql($dealerDB,1,$count_sql);
			$sum_page = ceil($count_query['count'] / $limit);
			$sum_count = $count_query['count'];

			foreach($list_query as $list_item)
			{
                $array['account_id'] = $list_item['account_id'];
				$create_time = $list_item['create_time'];
				$array['nickname'] = $list_item['nickname'];
				$array['avatar_url'] =  $list_item['headimgurl'];
				$array['member_id'] =  $list_item['member_id'];
				$array['status'] =  $list_item['status'];
                $array['is_admin'] =  $list_item['is_admin'];
				$result[] = $array;
			}
		}
		return array('result'=>OPT_CONST::SUCCESS,'data'=>$result, 'page'=>$page,'sum_page'=>$sum_page, 'sum_count'=>$sum_count,'result_message'=>"查找成员");
	}


	/*
		管理员处理成员
	*/
	public function dealMember($arrData)
    {
        $timestamp = time();
        $result = array();

        if (!isset($arrData['member_id']) || $arrData['member_id'] == G_CONST::EMPTY_STRING) {
            log_message('error', "function(dealMember):lack of member_id" . " in file" . __FILE__ . " on Line " . __LINE__);
            return $this->missingPrameterArr("member_id");
        }
        if (!isset($arrData['manager_id']) || $arrData['manager_id'] == G_CONST::EMPTY_STRING) {
            log_message('error', "function(dealMember):lack of manager_id" . " in file" . __FILE__ . " on Line " . __LINE__);
            return $this->missingPrameterArr("manager_id");
        }
        if (!isset($arrData['type']) || $arrData['type'] == G_CONST::EMPTY_STRING) {
            log_message('error', "function(dealMember):lack of type" . " in file" . __FILE__ . " on Line " . __LINE__);
            return $this->missingPrameterArr("type");
        }
        $dealerDB = Game_CONST::DBConst_Name;

        $member_id = $arrData['member_id'];
        $manager_id = $arrData['manager_id'];
        $type = $arrData['type'];

        //获取用户是否加入公会
        $join_where = 'member_id=' . $member_id . ' and is_delete=0';
        $join_sql = 'select manager_id,account_id,status from ' . Manage_Member . ' where ' . $join_where . ' limit 1';
        $join_query = $this->getDataBySql($dealerDB, 1, $join_sql);
        if (DB_CONST::DATA_NONEXISTENT != $join_query) {

            $updateMember_str = 'is_admin=0,update_time=' . $timestamp . ',update_appid="aid_' . $manager_id . '",status=' . $type;
            if ($type == 2 || $type == 3) {
                $updateMember_str .= ",is_delete=1";
            }

            $updateMember_where = 'member_id=' . $member_id;
            $updateMember_query = $this->changeNodeValue($dealerDB, Manage_Member, $updateMember_str, $updateMember_where);
            $result['status'] = $type;
            return array('result' => OPT_CONST::SUCCESS, 'data' => $result, 'result_message' => "操作成功");
        } else {
            return array('result' => OPT_CONST::FAILED, 'data' => $result, 'result_message' => "该成员不存在");
        }
    }
    public function changeAdmin($arrData)
    {
        $timestamp = time();
        $result = array();

        if (!isset($arrData['member_id']) || $arrData['member_id'] == G_CONST::EMPTY_STRING) {
            log_message('error', "function(changeAdmin):lack of member_id" . " in file" . __FILE__ . " on Line " . __LINE__);
            return $this->missingPrameterArr("member_id");
        }
        if (!isset($arrData['manager_id']) || $arrData['manager_id'] == G_CONST::EMPTY_STRING) {
            log_message('error', "function(changeAdmin):lack of manager_id" . " in file" . __FILE__ . " on Line " . __LINE__);
            return $this->missingPrameterArr("manager_id");
        }
        if (!isset($arrData['type']) || $arrData['type'] === G_CONST::EMPTY_STRING) {
            log_message('error', "function(changeAdmin):lack of type" . " in file" . __FILE__ . " on Line " . __LINE__);
            return $this->missingPrameterArr("type");
        }
        $dealerDB = Game_CONST::DBConst_Name;

        $member_id = $arrData['member_id'];
        $manager_id = $arrData['manager_id'];
        $type = $arrData['type'];

        //获取用户是否加入公会
        $join_where = 'member_id=' . $member_id . ' and is_delete=0 and status=1';
        $join_sql = 'select manager_id,account_id,status from ' . Manage_Member . ' where ' . $join_where . ' limit 1';
        $join_query = $this->getDataBySql($dealerDB, 1, $join_sql);
        if (DB_CONST::DATA_NONEXISTENT != $join_query) {
            $updateMember_str = 'update_time=' . $timestamp . ',update_appid="aid_' . $manager_id . '",is_admin=' . $type;
            $updateMember_where = 'member_id=' . $member_id;
            $updateMember_query = $this->changeNodeValue($dealerDB, Manage_Member, $updateMember_str, $updateMember_where);
            $result['is_admin'] = $type;
            return array('result' => OPT_CONST::SUCCESS, 'data' => $result, 'result_message' => "操作成功");
        } else {
            return array('result' => OPT_CONST::FAILED, 'data' => $result, 'result_message' => "该成员不存在");
        }
    }

}
